package net.bioteam.appweb;

import java.io.File;

import javax.persistence.EntityManagerFactory;

import net.bioteam.appweb.submit.CommandGenerator;
import net.bioteam.appweb.submit.CommandSubmitter;
import net.bioteam.appweb.submit.RunStatusChecker;
import net.bioteam.common.utils.MyEntityManager;
import net.sf.gilead.core.PersistentBeanManager;
import net.sf.gilead.core.hibernate.jpa.HibernateJpaUtil;

import org.apache.commons.io.FileUtils;
import org.ggf.drmaa.Session;
import org.ggf.drmaa.SessionFactory;

public class Config {
	
	/**
	 * Top Work directory for each user.
	 * Each user submission will be under topWorkdir+"/"+username+jobname
	 */
	public static String topWorkdir = "D:\\tmp";
	
	/**
	 * Top Directory for this context
	 */
	public static String topdir="/opt/cmdwork";
	
	/**
	 * drmmasession is used only if this is not null
	 */
	public static Session drmmasession;
	// should not be used.
	public static SessionFactory sf;

	public static String envstr;
	public static String perlProgramModifierStr = null;
	
	public static PersistentBeanManager beanManager;

	public static void init() throws Exception {
		envstr = FileUtils.readFileToString(new File(topdir+"/conf/env.sh"));
		if (sf != null) {
			
			drmmasession = sf.getSession();
			drmmasession.init("");
			
//			File file=new File(topdir+"/conf/sessioncontact");
//			if (file.exists())
//			{
//				String contact=IOUtils.toString(new FileReader(file));
//				drmmasession.init(contact);
//			} else
//			{
//				drmmasession.init("");
//				String contact=drmmasession.getContact();
//				FileWriter writer=new FileWriter(file);
//				writer.write(contact);
//				writer.close();
//			}
		}
		
		HibernateJpaUtil persistenceUtil = new HibernateJpaUtil();
		persistenceUtil.setEntityManagerFactory(MyEntityManager.emf);

		//HttpSessionProxyStore proxyStore = new HttpSessionProxyStore(); proxyStore.setPersistenceUtil(persistenceUtil);

		beanManager = new PersistentBeanManager();
		beanManager.setPersistenceUtil(persistenceUtil);
		//beanManager.setProxyStore(proxyStore);
		beanManager.setProxyStore(new net.bioteam.appweb.gwt.apps.server.NoStateProxyStore());
	}
	public static final String CATEGORY_KEY="_DRMAACATEGORY";
	public static final String NATIVE_KEY="_DRMAANATIVE";
	public static  final String EMAIL_KEY="_EMAIL";
	public static final String CLUSTER_KEY="_cluster";

	public static final boolean DEFAULT_ACTIVEUSER = true;
	
	/**
	 * temporary directory for end user to store uploaded file from web or web service
	 * Each user has its own temporary directory USER_TMP+"/"+username;
	 */
	public static String USER_TMP;
	
	/**
	 * directory to store user file in server. Each user's files are stored under
	 * USER_FILE+"/"+username
	 */
	public static String USER_FILE;
	public static CommandGenerator generator;
	public static CommandSubmitter submitter;
	public static RunStatusChecker checker;
	public static String DEFAULT_PROGRAM="blastall";
	
	
	public static String PUBLIC_FILE_DIR=".GLOBAL";
	public static String PUBLIC_TEMPLATE_DIR=".GLOBAL";
	public static String TEMPLATE_DIR="template";
	public static String PROGRAM_META_DIR="d:\\cmdportal\\cmdwork\\programmeta";

	
	
	public static String PARAMETER_MAP_DIR="d:\\cmdportal\\cmdwork\\parametermaps";

	//axis2 repo directory
	public static String repo_dir;
	
	//how many time we could try to submit the job
	public static int JOB_TRIAL_NUM=3;
	
	public static String ADMIN_EMAIL="jason@bioteam.net";
	
	//how many free job the end user can submit;
	public static int DEFAULT_DAILYQUOTE=5;
	
	public static String SGE_BIN_DIR="/opt/inquiry/sge/bin/lx24-amd64";
}
